package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcWebRtcMacMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcWebRtcMac;
import com.csun.cmny.provider.model.dto.webrtc.WebRtcMacQueryDto;
import com.csun.cmny.provider.service.DmcWebRtcMacService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2021-01-07 15:18
 * @Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcWebRtcMacServiceImpl extends BaseService<DmcWebRtcMac> implements DmcWebRtcMacService {

    @Resource
    private DmcWebRtcMacMapper dmcWebRtcMacMapper;

    /**
     * 查询mac列表 - 分页
     *
     * @param webRtcMacQueryDto the webrtc mac query dto
     *
     * @return the list.
     */
    @Override
    public List<DmcWebRtcMac> queryListWithPage(WebRtcMacQueryDto webRtcMacQueryDto) {

        PageHelper.startPage(webRtcMacQueryDto.getPageNum(), webRtcMacQueryDto.getPageSize());

        return dmcWebRtcMacMapper.queryListWithPage(webRtcMacQueryDto);
    }

    /**
     * 保存
     *
     * @param dmcWebRtcMac the dmc webrtc mac
     * @param loginAuthDto the login auth dto
     *
     * @return the int.
     */
    @Override
    public Integer save(DmcWebRtcMac dmcWebRtcMac, LoginAuthDto loginAuthDto) {

        dmcWebRtcMac.setUpdateInfo(loginAuthDto);

        // 判断mac是否重复
        Long id = dmcWebRtcMac.getId();
        String mac = dmcWebRtcMac.getMac();

        Example example = new Example(DmcWebRtcMac.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mac", mac);
        if (id != null) {
            criteria.andNotEqualTo("id", id);
        }

        int result = selectCountByExample(example);
        if (result > 0) {
            throw new DmcBizException(ErrorCodeEnum.DMC100621001);
        }

        if (dmcWebRtcMac.isNew()) {

            dmcWebRtcMacMapper.insertSelective(dmcWebRtcMac);
        }

        return dmcWebRtcMacMapper.updateByPrimaryKeySelective(dmcWebRtcMac);
    }

    /**
     * 根据Id删除
     *
     * @param id the id
     * @return the int.
     */
    @Override
    public Integer deleteById(Long id) {

        return dmcWebRtcMacMapper.deleteByPrimaryKey(id);
    }
}
